package com.fancy.learncenter.ui.view;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.CartoonKeyBean;
import com.fancy.learncenter.ui.adapter.CartoonKeyboardAdapter;
import com.fancy.learncenter.ui.callback.KeyboardAdapterImpl;
import com.fancy.learncenter.ui.callback.KeyboardViewImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * 键盘
 * Created by shiweiwei on 2017/12/27.
 */

public class CustomKeyboardView extends RelativeLayout {

    View view;
    Context mContext;
    RecyclerView recyclerView1;
    RecyclerView recyclerView2;
    RecyclerView recyclerView3;
    List<CartoonKeyBean> keyBeanList1;
    List<CartoonKeyBean> keyBeanList2;
    List<CartoonKeyBean> keyBeanList3;
    CartoonKeyboardAdapter adapter1;
    CartoonKeyboardAdapter adapter2;
    CartoonKeyboardAdapter adapter3;
    ImageView upperImage;
    ImageView okImage;
    boolean isLower = true;
    RelativeLayout douhao;

    public KeyboardViewImpl getKeyboardClick() {
        return keyboardClick;
    }

    public void setKeyboardClick(KeyboardViewImpl keyboardClick) {
        this.keyboardClick = keyboardClick;
    }

    KeyboardViewImpl keyboardClick;

    public CustomKeyboardView(Context context) {
        super(context);
        this.mContext = context;
        initView();
        initData();
    }

    public CustomKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
        initData();
    }

    private void initData() {
        initData1();
        initData2();
        initData3();
    }

    private void initView() {

        view = LayoutInflater.from(mContext).inflate(R.layout.custom_keyboard_layout, this);
        recyclerView1 = (RecyclerView) view.findViewById(R.id.recyclerView_1);
        recyclerView2 = (RecyclerView) view.findViewById(R.id.recyclerView_2);
        recyclerView3 = (RecyclerView) view.findViewById(R.id.recyclerView_3);
        upperImage = (ImageView) view.findViewById(R.id.upper_lower);
        okImage = (ImageView) view.findViewById(R.id.ok_image);
        douhao = view.findViewById(R.id.douhao);

        douhao.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (keyboardClick != null) {
                    keyboardClick.onClick("'");
                }
            }
        });

        upperImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                isLower = !isLower;
                if (isLower) {
                    for (CartoonKeyBean bean : keyBeanList1) {
                        bean.setLower(true);
                    }
                    for (CartoonKeyBean bean : keyBeanList2) {
                        bean.setLower(true);
                    }
                    for (CartoonKeyBean bean : keyBeanList3) {
                        bean.setLower(true);
                    }
                    adapter1.notifyDataSetChanged();
                    adapter2.notifyDataSetChanged();
                    adapter3.notifyDataSetChanged();
                    upperImage.setImageResource(R.mipmap.cartoon_keyboard_lower);

                } else {
                    for (CartoonKeyBean bean : keyBeanList1) {
                        bean.setLower(false);
                    }
                    for (CartoonKeyBean bean : keyBeanList2) {
                        bean.setLower(false);
                    }
                    for (CartoonKeyBean bean : keyBeanList3) {
                        bean.setLower(false);
                    }
                    adapter1.notifyDataSetChanged();
                    adapter2.notifyDataSetChanged();
                    adapter3.notifyDataSetChanged();
                    upperImage.setImageResource(R.mipmap.cartoon_keyboard_upper);
                }
            }
        });

        okImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                keyboardClick.okClick();
            }
        });
    }


    private void initData2() {

        keyBeanList2 = new ArrayList<>();
        keyBeanList3 = new ArrayList<>();

        CartoonKeyBean bean1 = new CartoonKeyBean();
        bean1.setLower(true);
        bean1.setLowerValue("a");
        bean1.setUpperValue("A");
        keyBeanList2.add(bean1);
        CartoonKeyBean bean2 = new CartoonKeyBean();
        bean2.setLower(true);
        bean2.setLowerValue("s");
        bean2.setUpperValue("S");
        keyBeanList2.add(bean2);
        CartoonKeyBean bean3 = new CartoonKeyBean();
        bean3.setLower(true);
        bean3.setLowerValue("d");
        bean3.setUpperValue("D");
        keyBeanList2.add(bean3);
        CartoonKeyBean bean4 = new CartoonKeyBean();
        bean4.setLower(true);
        bean4.setLowerValue("f");
        bean4.setUpperValue("F");
        keyBeanList2.add(bean4);
        CartoonKeyBean bean5 = new CartoonKeyBean();
        bean5.setLower(true);
        bean5.setLowerValue("g");
        bean5.setUpperValue("G");
        keyBeanList2.add(bean5);
        CartoonKeyBean bean6 = new CartoonKeyBean();
        bean6.setLower(true);
        bean6.setLowerValue("h");
        bean6.setUpperValue("H");
        keyBeanList2.add(bean6);
        CartoonKeyBean bean7 = new CartoonKeyBean();
        bean7.setLower(true);
        bean7.setLowerValue("j");
        bean7.setUpperValue("J");
        keyBeanList2.add(bean7);
        CartoonKeyBean bean8 = new CartoonKeyBean();
        bean8.setLower(true);
        bean8.setLowerValue("k");
        bean8.setUpperValue("K");
        keyBeanList2.add(bean8);
        CartoonKeyBean bean9 = new CartoonKeyBean();
        bean9.setLower(true);
        bean9.setLowerValue("l");
        bean9.setUpperValue("L");
        keyBeanList2.add(bean9);


        GridLayoutManager manager2 = new GridLayoutManager(mContext, 9);
        manager2.setOrientation(GridLayoutManager.VERTICAL);
        recyclerView2.setLayoutManager(manager2);
        adapter2 = new CartoonKeyboardAdapter(mContext, keyBeanList2);
        recyclerView2.setAdapter(adapter2);
        adapter2.setListener(new KeyboardAdapterImpl() {
            @Override
            public void onClick(String keyValue) {

                if (keyboardClick != null) {
                    keyboardClick.onClick(keyValue);
                }
            }
        });
    }

    private void initData1() {

        keyBeanList1 = new ArrayList<>();
        CartoonKeyBean bean1 = new CartoonKeyBean();
        bean1.setUpperValue("Q");
        bean1.setLower(true);
        bean1.setLowerValue("q");
        keyBeanList1.add(bean1);
        CartoonKeyBean bean2 = new CartoonKeyBean();
        bean2.setLowerValue("w");
        bean2.setUpperValue("W");
        bean2.setLower(true);
        keyBeanList1.add(bean2);
        CartoonKeyBean bean3 = new CartoonKeyBean();
        bean3.setLowerValue("e");
        bean3.setUpperValue("E");
        bean3.setLower(true);
        keyBeanList1.add(bean3);
        CartoonKeyBean bean4 = new CartoonKeyBean();
        bean4.setLower(true);
        bean4.setLowerValue("r");
        bean4.setUpperValue("R");
        keyBeanList1.add(bean4);
        CartoonKeyBean bean5 = new CartoonKeyBean();
        bean5.setUpperValue("T");
        bean5.setLowerValue("t");
        bean5.setLower(true);
        keyBeanList1.add(bean5);
        CartoonKeyBean bean6 = new CartoonKeyBean();
        bean6.setUpperValue("Y");
        bean6.setLowerValue("y");
        bean6.setLower(true);
        keyBeanList1.add(bean6);
        CartoonKeyBean bean7 = new CartoonKeyBean();
        bean7.setLower(true);
        bean7.setLowerValue("u");
        bean7.setUpperValue("U");
        keyBeanList1.add(bean7);
        CartoonKeyBean bean8 = new CartoonKeyBean();
        bean8.setLower(true);
        bean8.setUpperValue("I");
        bean8.setLowerValue("i");
        keyBeanList1.add(bean8);
        CartoonKeyBean bean9 = new CartoonKeyBean();
        bean9.setLower(true);
        bean9.setLowerValue("o");
        bean9.setUpperValue("O");
        keyBeanList1.add(bean9);
        CartoonKeyBean bean10 = new CartoonKeyBean();
        bean10.setLower(true);
        bean10.setLowerValue("p");
        bean10.setUpperValue("P");
        keyBeanList1.add(bean10);

        GridLayoutManager manager = new GridLayoutManager(mContext, 10);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        recyclerView1.setLayoutManager(manager);
        adapter1 = new CartoonKeyboardAdapter(mContext, keyBeanList1);
        recyclerView1.setAdapter(adapter1);
        adapter1.setListener(new KeyboardAdapterImpl() {
            @Override
            public void onClick(String keyValue) {

                if (keyboardClick != null) {
                    keyboardClick.onClick(keyValue);
                }
            }
        });
    }

    private void initData3() {

        CartoonKeyBean bean1 = new CartoonKeyBean();
        bean1.setLower(true);
        bean1.setUpperValue("Z");
        bean1.setLowerValue("z");
        keyBeanList3.add(bean1);
        CartoonKeyBean bean2 = new CartoonKeyBean();
        bean2.setLower(true);
        bean2.setLowerValue("x");
        bean2.setUpperValue("X");
        keyBeanList3.add(bean2);
        CartoonKeyBean bean3 = new CartoonKeyBean();
        bean3.setLower(true);
        bean3.setUpperValue("C");
        bean3.setLowerValue("c");
        keyBeanList3.add(bean3);
        CartoonKeyBean bean4 = new CartoonKeyBean();
        bean4.setLower(true);
        bean4.setLowerValue("v");
        bean4.setUpperValue("V");
        keyBeanList3.add(bean4);
        CartoonKeyBean bean5 = new CartoonKeyBean();
        bean5.setLower(true);
        bean5.setLowerValue("b");
        bean5.setUpperValue("B");
        keyBeanList3.add(bean5);
        CartoonKeyBean bean6 = new CartoonKeyBean();
        bean6.setLower(true);
        bean6.setLowerValue("n");
        bean6.setUpperValue("N");
        keyBeanList3.add(bean6);
        CartoonKeyBean bean7 = new CartoonKeyBean();
        bean7.setLower(true);
        bean7.setLowerValue("m");
        bean7.setUpperValue("M");
        keyBeanList3.add(bean7);
//        CartoonKeyBean bean8 = new CartoonKeyBean();
//        bean8.setUpperValue("'");
//        bean8.setLowerValue("'");
//        bean8.setLower(true);
//        keyBeanList3.add(bean8);
        CartoonKeyBean bean9 = new CartoonKeyBean();
        bean9.setLower(true);
        bean9.setLowerValue("delete");
        bean9.setUpperValue("delete");
        keyBeanList3.add(bean9);

        GridLayoutManager manager3 = new GridLayoutManager(mContext, 8);
        manager3.setOrientation(GridLayoutManager.VERTICAL);
        recyclerView3.setLayoutManager(manager3);
        adapter3 = new CartoonKeyboardAdapter(mContext, keyBeanList3);
        recyclerView3.setAdapter(adapter3);
        adapter3.setListener(new KeyboardAdapterImpl() {
            @Override
            public void onClick(String keyValue) {
                if ("delete".equals(keyValue)) {
                    keyboardClick.deleteClick();
                } else {
                    keyboardClick.onClick(keyValue);
                }
            }
        });
    }


}
